package com.htsc.mrp.utils;

import java.io.File;
import java.io.FileWriter;

/**
 * 生成dao接口实现类
 *
 * @author huqi
 * @create-time March 20, 2019 14:06:29
 */
public class CreateDaoImplUtil {

    public synchronized static void createDaoImpl(
            String daoTargetPackage,
            String modelTargetPackage,
            String dbTableName,
            String daoAddress
    ) throws Exception {
        String modelName = StringUtil.lineToHump(dbTableName);
        String source = "package " + daoTargetPackage + ".impl;\n" +
                "\n" +
                "import " + daoTargetPackage + ".I" + modelName + "Dao;\n" +
                "import " + modelTargetPackage + "." + modelName + ";\n" +
                "import " + daoTargetPackage + ".BaseDao;\n" +
                "import org.springframework.stereotype.Repository;\n" +
                "\n" +
                "/**\n" +
                " * @author K0890042\n" +
                " * @date 2019/3/20 15:36\n" +
                " * @description: Htsc.com Inc. Copyright (c) 2004-2019 All Rights Reserved.\n" +
                " */\n" +
                "@Repository\n" +
                "public class " + modelName + "Dao extends BaseDao implements I" + modelName + "Dao {\n" +
                "    @Override\n" +
                "    public int deleteByPrimaryKey(Integer id) {\n" +
                "        return delete(\"" + modelName + "Mapper.deleteByPrimaryKey\", id);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int insert(" + modelName + " record) {\n" +
                "        return insert(\"" + modelName + "Mapper.insert\", record);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int insertSelective(" + modelName + " record) {\n" +
                "        return insert(\"" + modelName + "Mapper.insertSelective\", record);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public " + modelName + " selectByPrimaryKey(Integer id) {\n" +
                "        return selectByPrimaryKey(id);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int updateByPrimaryKeySelective(" + modelName + " record) {\n" +
                "        return update(\"" + modelName + "Mapper.updateByPrimaryKeySelective\", record);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int updateByPrimaryKey(" + modelName + " record) {\n" +
                "        return update(\"" + modelName + "Mapper.updateByPrimaryKey\", record);\n" +
                "    }\n" +
                "}\n";
        String fileName = daoAddress.replace("\\", "/") + "/impl/" + modelName + "Dao.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write(source);
        fw.flush();
        fw.close();
    }

}